package com.jzr.bedside.presenter.impl

import android.content.Context
import com.jzr.bedside.api.Api
import com.jzr.bedside.base.BasePresenter
import com.jzr.bedside.bean.BedInfoBean
import com.jzr.bedside.bean.SatisfySurveyListBean
import com.jzr.bedside.bean.TQuestionBean
import com.jzr.bedside.bean.body.AddSatisfyBean
import com.jzr.bedside.presenter.contract.EvaluationActivityContract
import okhttp3.RequestBody
import rx.Observer
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

class EvaluationActivityImpl: BasePresenter<EvaluationActivityContract.View>(),
    EvaluationActivityContract.Presenter<EvaluationActivityContract.View>  {


    override fun getSatisfySurveyList(context: Context?) {
        showLoadingDialog(context!!)
        addSubscrebe(
            Api.getInstance()?.getSatisfySurveyList()
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<TQuestionBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        if (mView != null) mView?.showError(e.toString())
                    }

                    override fun onNext(t: TQuestionBean?) {
                        dismissLoadingDialog()
                        if (mView != null && t != null && t.code == 0) {
                            mView?.getSatisfySurveyListSuccess(t)
                        } else {
                            mView?.showError(t?.msg)
                        }
                    }
                })
        )
    }

    override fun getBedsideSatisfySurveyAdd(context: Context?, body: RequestBody) {
        showLoadingDialog(context!!)
        addSubscrebe(
            Api.getInstance()?.getBedsideSatisfySurveyAdd(body)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<TQuestionBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        if (mView != null) mView?.showError(e.toString())
                    }

                    override fun onNext(t: TQuestionBean?) {
                        dismissLoadingDialog()
                        if (mView != null && t != null && t.code == 0) {
                            mView?.getBedsideSatisfySurveyAddSuccess(t)
                        } else {
                            mView?.showError(t?.msg)
                        }
                    }
                })
        )
    }
}